
import optparse #解析命令行命令模块
import socketserver

from conf import settings
from core import server


class ArgvHandler():
    def __init__(self):
        self.op=optparse.OptionParser()
        self.op.add_option("-s","--server",dest="server")  #接手命令参数
        self.op.add_option("-P","--port",dest="port")

        options,args=self.op.parse_args()

        # print(options) # {'server': '127.0.0.1', 'port': '8080'}
        # print(args)
        self.verify_args(options,args)

    def verify_args(self,options,args):
        cmd=args[0]
        if hasattr(self,cmd): #反射
            func=getattr(self,cmd)
            func()
        
    def start(self):
        print("这个服务已经启动")
        s=socketserver.ThreadingTCPServer((settings.IP,settings.PORT),server.ServerHandler)
        s.serve_forever()